#include "EPubBufferImp.h"

EPubBufferImp::EPubBufferImp()
	: m_Buffer(NULL)
	, m_BufferSize(0)
{
}

EPubBufferImp::EPubBufferImp(EPUB_Size size)
	: m_Buffer(NULL)
	, m_BufferSize(size)
{
	m_Buffer = m_Allocator.Allocate(m_BufferSize);
	memset(m_Buffer, 0, m_BufferSize * sizeof(EPUB_Byte));
}

EPubBufferImp::EPubBufferImp(const EPUB_Byte *buffer, EPUB_Size size)
	: m_Buffer(NULL)
	, m_BufferSize(size)
{
	m_Buffer = m_Allocator.Allocate(m_BufferSize);
	memset(m_Buffer, 0, m_BufferSize * sizeof(EPUB_Byte));
	memcpy(m_Buffer, buffer, m_BufferSize * sizeof(EPUB_Byte));
}

EPubBufferImp::EPubBufferImp(const IEPubBuffer &other)
	: m_Buffer(NULL)
	, m_BufferSize(other.GetBufferSize())
{
	m_Buffer = m_Allocator.Allocate(m_BufferSize);
	memset(m_Buffer, 0, m_BufferSize * sizeof(EPUB_Byte));
	memcpy(m_Buffer, other.GetBuffer(), m_BufferSize * sizeof(EPUB_Byte));
}

EPubBufferImp::~EPubBufferImp()
{
	Clear();
}

void EPubBufferImp::SetBufferSize(EPUB_Size size)
{
	if(m_BufferSize == size) {
		return;
	}

	Clear();

	m_BufferSize = size;
	m_Buffer = m_Allocator.Allocate(m_BufferSize);
	memset(m_Buffer, 0, m_BufferSize * sizeof(EPUB_Byte));
}

void EPubBufferImp::SetBuffer(const EPUB_Byte *buffer, EPUB_Size size)
{
	SetBufferSize(size);
	memcpy(m_Buffer, buffer, m_BufferSize * sizeof(EPUB_Byte));
}

void EPubBufferImp::SetBuffer(const IEPubBuffer &other)
{
	SetBufferSize(other.GetBufferSize());
	memcpy(m_Buffer, other.GetBuffer(), m_BufferSize * sizeof(EPUB_Byte));
}

void EPubBufferImp::Clear()
{
	if(m_Buffer) {
		m_Allocator.Deallocate(m_Buffer);
		m_Buffer = NULL;
		m_BufferSize = 0;
	}
}

IEPubBuffer &EPubBufferImp::operator =(const IEPubBuffer &other)
{
	return *this;
}